//
//  ShareCustom.m
//  点击右边的barbutton
//
//  Created by jdc on 16/8/20.
//  Copyright © 2016年 yjq. All rights reserved.
//

#import "ShareCustom.h"
//#import "WXApi.h"
//设备物理大小
#define kScreenWidth   [UIScreen mainScreen].bounds.size.width
#define kScreenHeight  [UIScreen mainScreen].bounds.size.height
#define SYSTEM_VERSION   [[UIDevice currentDevice].systemVersion floatValue]
//屏幕宽度相对iPhone6屏幕宽度的比例
#define KWidth_Scale    [UIScreen mainScreen].bounds.size.width/640.0f

@implementation ShareCustom

static id _publishContent;//类方法中的全局变量这样用（类型前面加static）

/*
 自定义的分享类，使用的是类方法，其他地方只要 构造分享内容publishContent就行了
 */

+(void)shareWithContent:(id)publishContent/*只需要在分享按钮事件中 构建好分享内容publishContent传过来就好了*/
{
    _publishContent = publishContent;
    
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    
    UIView *blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    
    blackView.backgroundColor = [UIColor colorWithRed:68/255.0 green:68/255.0 blue:68/255.0 alpha:0.85f];
    
    blackView.tag = 440;
    
    [window addSubview:blackView];
    
    UIView *shareView = [[UIView alloc] initWithFrame:CGRectMake(0,(kScreenHeight-450*KWidth_Scale), window.bounds.size.width, 450*KWidth_Scale)];
    //shareView.backgroundColor = [UIColor colorWithString:@"f6f6f6"];
    shareView.backgroundColor = [UIColor colorWithRed:225/255.0 green:225/255.0 blue:225/255.0 alpha:1.0];
    
    shareView.tag = 441;
    [window addSubview:shareView];
    
    
    NSArray *btnImages = @[@"share_weixin", @"share_pyq",@"share_qq", @"share_dx", @"share_weibo"];
    
    NSArray *btnTitles = @[@"微信", @"微信朋友圈",@"QQ",@"短信",@"微博"];
    for (NSInteger i=0; i<[btnImages count]; i++) {
        CGFloat top = 0.0f;
        top = 24*KWidth_Scale;
        if(i==4){
            i=0;
            top = 24*KWidth_Scale;
            UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(32*KWidth_Scale+i*104*KWidth_Scale + 52 * KWidth_Scale * i, top+105, 104*KWidth_Scale, 104*KWidth_Scale)];
        
            [button setImage:[UIImage imageNamed:btnImages[4]] forState:UIControlStateNormal];
            button.tag = 331+4;
            [button addTarget:self action:@selector(shareBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [shareView addSubview:button];
            //45*KWidth_Scale+i*104*KWidth_Scale + 32 * KWidth_Scale * i
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(25*KWidth_Scale+i*98*KWidth_Scale + 54 * KWidth_Scale * i-2, top + 110*KWidth_Scale+105, 124*KWidth_Scale, 28 * KWidth_Scale)];
            label.text = btnTitles[4];
            label.font = [UIFont systemFontOfSize:24*KWidth_Scale];
            label.textColor = [UIColor colorWithRed:138/255.0 green:138/255.0 blue:138/255.0 alpha:1.0];
            label.textAlignment = NSTextAlignmentCenter;
            [shareView addSubview:label];
            break;
        }
        
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(32*KWidth_Scale+i*104*KWidth_Scale + 52 * KWidth_Scale * i, top+20, 104*KWidth_Scale, 104*KWidth_Scale)];
        
        [button setImage:[UIImage imageNamed:btnImages[i]] forState:UIControlStateNormal];
        //45*KWidth_Scale+i*104*KWidth_Scale + 32 * KWidth_Scale * i
        if(i==0){
            UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(32*KWidth_Scale+i*98*KWidth_Scale + 54 * KWidth_Scale * i-6, top + 110*KWidth_Scale+20 , 124*KWidth_Scale, 28 * KWidth_Scale)];
            label.text = btnTitles[i];
            label.font = [UIFont systemFontOfSize:24*KWidth_Scale];
            label.textColor = [UIColor colorWithRed:138/255.0 green:138/255.0 blue:138/255.0 alpha:1.0];
            label.textAlignment = NSTextAlignmentCenter;
            [shareView addSubview:label];
            
            button.tag = 331+i;
            [button addTarget:self action:@selector(shareBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [shareView addSubview:button];
            continue;
        }
        UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(32*KWidth_Scale+i*98*KWidth_Scale + 54 * KWidth_Scale * i, top + 110*KWidth_Scale+20 , 124*KWidth_Scale, 28 * KWidth_Scale)];
        label.text = btnTitles[i];
        label.font = [UIFont systemFontOfSize:24*KWidth_Scale];
        label.textColor = [UIColor colorWithRed:138/255.0 green:138/255.0 blue:138/255.0 alpha:1.0];
        label.textAlignment = NSTextAlignmentCenter;
        [shareView addSubview:label];
       
        
        button.tag = 331+i;
        [button addTarget:self action:@selector(shareBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [shareView addSubview:button];
    }
    UILabel * shareLabel = [[UILabel alloc]initWithFrame:CGRectMake(window.bounds.size.width*0.43, 8 , 124*KWidth_Scale, 28 * KWidth_Scale)];
    shareLabel.font = [UIFont systemFontOfSize:28*KWidth_Scale];
    shareLabel.text = @"分享到";
    [shareView addSubview:shareLabel];
    UIButton *cancleBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, shareView.frame.size.height-80*KWidth_Scale,window.bounds.size.width, 80*KWidth_Scale)];
    //    [cancleBtn setBackgroundImage:[UIImage imageNamed:@"IOS-取消@2x.png"] forState:UIControlStateNormal];
    cancleBtn.tag = 339;
    
    [cancleBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    
    cancleBtn.backgroundColor = [UIColor colorWithHue:246 saturation:246 brightness:246 alpha:1];
    cancleBtn.backgroundColor = [UIColor grayColor];//
    [cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancleBtn addTarget:self action:@selector(shareBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [shareView addSubview:cancleBtn];
    
    shareView.frame = CGRectMake(0,(kScreenHeight-450*KWidth_Scale), window.bounds.size.width, 450*KWidth_Scale);
    ;
    
    blackView.alpha = 0;
    [UIView animateWithDuration:0.35f animations:^{
        //        shareView.transform = CGAffineTransformMakeScale(1, 1);
        
        shareView.frame = CGRectMake(0,(kScreenHeight-450*KWidth_Scale), window.bounds.size.width, 450*KWidth_Scale);
        blackView.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
}

+(void)shareBtnClick:(UIButton *)btn
{
    //    NSLog(@"%@",[ShareSDK version]);
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    UIView *blackView = [window viewWithTag:440];
    UIView *shareView = [window viewWithTag:441];
    
    //为了弹窗不那么生硬，这里加了个简单的动画
    //    shareView.transform = CGAffineTransformMakeScale(1, 1);
    [UIView animateWithDuration:0.35f animations:^{
        //        shareView.transform = CGAffineTransformMakeScale(1/300.0f, 1/270.0f);
        blackView.alpha = 0;
        shareView.frame = CGRectMake(0,(kScreenHeight-400*KWidth_Scale), window.bounds.size.width, 400*KWidth_Scale);
        
    } completion:^(BOOL finished) {
        
        [shareView removeFromSuperview];
        [blackView removeFromSuperview];
    }];
//    int shareType = 0;
//    id publishContent = _publishContent;
    switch (btn.tag) {
        case 331:
        {
            //            shareType = ShareTypeWeixiSession;
            NSLog(@"微信好友");
        }
            break;
            
        case 332:
        {
            NSLog(@"微信朋友圈");
            
            //            shareType = ShareTypeWeixiTimeline;
        }
            break;
        case 333:
        {
            NSLog(@"QQ");
            
            //            shareType = ShareTypeWeixiTimeline;
        }
            break;
        case 334:
        {
            NSLog(@"短信");
            
            //            shareType = ShareTypeWeixiTimeline;
        }
            break;
        case 335:
        {
            NSLog(@"微博");
            
            //            shareType = ShareTypeWeixiTimeline;
        }
            break;
         case 339:
        {
            NSLog(@"取消");
            
        }
            break;
            
        default:
            break;
  
}
}
@end
